package cn.zh.stan.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.ITem.ReItem;
import cn.zh.stan.R;
import cn.zh.stan.activity.AddAdress;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by WYN on 2018/1/31 0031.
 */

public class ReAdapter extends BaseAdapter{

    Context context;
    ArrayList<ReItem> arrayList;
    LayoutInflater inflater;
    Dialog dialog;
    String uid;

    public ReAdapter(Context context,ArrayList<ReItem> arrayList){
        this.context=context;
        this.arrayList=arrayList;
        inflater=LayoutInflater.from(context);
        }


    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return arrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Holder holder=null;
        if (null==view){
            holder=new Holder();
            view=inflater.inflate(R.layout.re_layout,null,false);
            holder.tvname=view.findViewById(R.id.tvname);
            holder.phon=view.findViewById(R.id.phon);
            holder.compy=view.findViewById(R.id.compy);
            holder.adres=view.findViewById(R.id.adres);
            holder.txtedit=view.findViewById(R.id.txtedit);
            holder.dele=view.findViewById(R.id.dele);
            holder.sdradio=view.findViewById(R.id.sdradio);
           view.setTag(holder);
          }else {
            holder= (Holder) view.getTag();
           }
           holder.tvname.setText(arrayList.get(i).getPersonName());
           holder.phon.setText(arrayList.get(i).getMoblie());
           holder.compy.setText(arrayList.get(i).getCompanyName());
           holder.adres.setText(arrayList.get(i).getProvinceName()+arrayList.get(i).getCityName()+arrayList.get(i).getCountyName()+arrayList.get(i).getAddress());
         if (arrayList.get(i).getIsDefault().equals("1")){
             holder.sdradio.setChecked(true);
         }else {
             holder.sdradio.setChecked(false);
         }

        holder.dele.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                getDialog(i);
              }
          });
        holder.txtedit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, AddAdress.class);
                intent.putExtra("type","1");
                intent.putExtra("Item",arrayList.get(i));
                context.startActivity(intent);
              }
           });
        holder.sdradio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    String id=arrayList.get(i).getId();
                    setdefalt(id);
                }
               }
        });

        return view;
        }


    class Holder{
      TextView tvname,phon,compy,adres,txtedit,dele;
      RadioButton sdradio;
      }


    public void getDialog(final int position){
        View v = inflater.inflate(R.layout.delete, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v
                .findViewById(R.id.delete);// 加载布局

        dialog = new Dialog(context, R.style.MyDialogStyle);
        dialog.show();
        dialog.setContentView(layout,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        v.findViewById(R.id.ocancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
              }
          });
        v.findViewById(R.id.osure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //s删除地址网络请求
                String id=arrayList.get(position).getId();
                dadress(id,position);
                }
              });
             }


         public void dadress(String id, final int position){
             OkHttpUtils.post(BaseUrl.delt_adress)
                     .params("id",id)
                     .execute(new StringCallback() {
                         @Override
                         public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                             try {
                                 JSONObject js=new JSONObject(s);
                                 boolean State=js.getBoolean("State");
                                 if (State){
                                     arrayList.remove(position);
                                     ToastUtil.showLong(context,"删除成功");
                                     notifyDataSetChanged();
                                 }
                                 dialog.dismiss();
                             } catch (JSONException e) {
                                 e.printStackTrace();
                             }
                           }
                         @Override
                         public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                             super.onError(isFromCache, call, response, e);
                         }
                     });
                  }


         public void setdefalt(final String id){
             OkHttpUtils.post(BaseUrl.default_adress)
                     .params("id",id)
                     .params("uid",uid)
                     .execute(new StringCallback() {
                         @Override
                         public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                             System.out.println("------>设为默认地址==="+s);
                             try {
                                 JSONObject js=new JSONObject(s);
                                 boolean State=js.getBoolean("State");
                                 if (State){
                                    boolean Flag=js.getBoolean("Flag");


                                    }
                              } catch (JSONException e) {
                                 e.printStackTrace();
                             }
                         }
                         @Override
                         public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                             super.onError(isFromCache, call, response, e);
                         }
                       });
                   }


    public void getAdrss(){

        OkHttpUtils.get(BaseUrl.adress_list)
                .params("userId",uid)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        //获取用户地址列表
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                                JSONArray ja=js.getJSONArray("Res");
                                arrayList.clear();
                                for (int i=0;i<ja.length();i++){
                                    JSONObject jo=ja.getJSONObject(i);

                                    String Id =jo.getString("Id");
                                    String MemberId =jo.getString("MemberId");
                                    String ProvinceId =jo.getString("ProvinceId");
                                    String ProvinceName =jo.getString("ProvinceName");
                                    String CityId =jo.getString("CityId");
                                    String CityName =jo.getString("CityName");
                                    String CountyId =jo.getString("CountyId");
                                    String CountyName =jo.getString("CountyName");
                                    String CompanyName =jo.getString("CompanyName");
                                    String PersonName =jo.getString("PersonName");
                                    String Address =jo.getString("Address");
                                    String Moblie =jo.getString("Moblie");
                                    String IsDefault =jo.getString("IsDefault");
                                    String CreateTime =jo.getString("CreateTime");
                                    ReItem reItem=new ReItem(Id,MemberId,ProvinceId,ProvinceName,
                                            CityId,CityName,CountyId,CountyName,CompanyName,PersonName,
                                            Address,Moblie,IsDefault,CreateTime);
                                    arrayList.add(reItem);
                                }
                                notifyDataSetChanged();

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
    }



}
